package com.alatus.springAI.Service.impl;

import com.alatus.springAI.Bean.*;
import com.alatus.springAI.Service.ExcelService;
import com.alatus.springAI.SpringAiApplication;
import com.alatus.springAI.Listener.UploadDataListener;
import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class ExcelServiceImpl implements ExcelService {
    @Override
    public void importExcel(InputStream inputStream) {
//        第一个参数是文件,第二个参数是excel模板类,第三个是监听器
        EasyExcel.read(inputStream, ProductDetails.class,new UploadDataListener()).sheet().doRead();
    }
    @Override
    public List<WalmartProduct> getSkuInfo() {
        List<WalmartProduct> walmartProductExcelList = new ArrayList<>();
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        filteredProductDetails.forEach(productDetail -> {
            WalmartProduct walmartProduct = new WalmartProduct();
//            设置sku,商品名,品牌名,制造商代码
            if(SpringAiApplication.shopName!=null){
                walmartProduct.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
                walmartProduct.setProductName(getValue(productDetail.getTitle())+" "+SpringAiApplication.shopName);
                walmartProduct.setBrand(SpringAiApplication.shopName);
//            跨界联名
                walmartProduct.setBrandLicense(SpringAiApplication.shopName);
//                设置制造商信息
                walmartProduct.setManufacturerName(SpringAiApplication.shopName);
//                设置制造商代码
                walmartProduct.setManufacturerPartNumber(SpringAiApplication.shopName+" 2024");
            }
            else if(SpringAiApplication.shopId!=null){
                walmartProduct.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
                walmartProduct.setProductName(productDetail.getTitle()+" "+SpringAiApplication.shopId);
                walmartProduct.setBrand(SpringAiApplication.shopId);
//            跨界联名
                walmartProduct.setBrandLicense(SpringAiApplication.shopId);
//                设置制造商信息
                walmartProduct.setManufacturerName(SpringAiApplication.shopId);
//                设置制造商代码
                walmartProduct.setManufacturerPartNumber(SpringAiApplication.shopId+" 2024");
            }
            else{
                walmartProduct.setSku(getValue(productDetail.getSKU()));
                walmartProduct.setProductName(getValue(productDetail.getTitle()));
                walmartProduct.setBrand("请完善店铺信息");
                walmartProduct.setBrandLicense("");
                walmartProduct.setManufacturerName("请完善店铺信息");
//                设置制造商代码
                walmartProduct.setManufacturerPartNumber(getValue(productDetail.getTitle()).substring(0,54)+" 2024");
            }
//          productDetail.getWalmartSuggestedPriceUSD() 返回的是一个包含价格信息的字符串
            String walmartSuggestedPriceUSD = productDetail.getWalmartSuggestedPriceUSD();
//          更灵活的正则表达式，匹配所有数字和小数点组合，直到遇见 "USD"
            Pattern pattern = Pattern.compile("(\\d+\\.\\d+) USD");
            Matcher matcher = pattern.matcher(walmartSuggestedPriceUSD);
            Double walmartSuggestedPrice = 0.0;
            if (matcher.find()) {
                // 获取匹配到的数字
                walmartSuggestedPrice = Double.parseDouble(matcher.group(1));
            }
//          设置到 walmartProduct
            walmartProduct.setSellingPrice(walmartSuggestedPrice);
//            货运重量(乘以0.0022)保留两位
            Double convertedWeight = Double.parseDouble(productDetail.getWeightGram()) * 0.0022;
            // 使用DecimalFormat保留两位小数
            DecimalFormat df = new DecimalFormat("#.##");
            String formattedWeight = df.format(convertedWeight);
            walmartProduct.setShippingWeight(Double.parseDouble(formattedWeight));
//            主图(第二张图,白底图)
            walmartProduct.setMainImageURL(getValue(productDetail.getImageURL2()));
//            设置网站描述(亚马逊描述)
            walmartProduct.setSiteDescription(getValue(productDetail.getAmazonDescription()));
//            牛仔裤样式
            if(getValue(productDetail.getTitle()).contains("jean")){
//                全部写上增加曝光度
                walmartProduct.setJeanStyle("Straight Leg Jeans,Skinny Jeans,Bootcut Jeans,High Waist Jeans,High Waist Jeans,Slim Fit Jeans,Tapered Jeans,Mom Jeans,Cropped Jeans,Flared Jeans");
            }
//            是否怀孕可穿
            if(productDetail.getTitle().contains("pregant")|| productDetail.getFivePointDescription().contains("pregant")){
                walmartProduct.setIsMaternity("Yes");
            }
            else{
                walmartProduct.setIsMaternity("No");
            }
//            尺码设置为美码
            walmartProduct.setSockSize(getValue(productDetail.getUsSize()));
//            服装风格?
            walmartProduct.setCharacter("Human Being");
//            服装重量(小于半斤为轻,小于一斤为中等,大于为重)
            if(Double.parseDouble(productDetail.getWeightGram())<250){
                walmartProduct.setClothingWeight("Lightweight");
            }
            else if(Double.parseDouble(productDetail.getWeightGram())<500){
                walmartProduct.setClothingWeight("Mid-Weight");
            }
            else{
                walmartProduct.setClothingWeight("Heavyweight");
            }
//            设置服装适合的天气状况
            walmartProduct.setWeather("Sun,Snow,Wind,Rain,Fog,All-Weather");
//            设置服装适合的季节,为了提高曝光全季节
            walmartProduct.setSeason("Spring");
            walmartProduct.setSeason1("Summer");
            walmartProduct.setSeason2("Autumn");
            walmartProduct.setSeason3("Winter");
//            设置腰线位置
            walmartProduct.setWaistRise("High,Low,Mid,Ultra-Low");
//            设置毛衣的款式
            if(getValue(productDetail.getTitle()).contains("Sweater")){
//                walmartProduct.setSweaterStyle("");
            }
            if(getValue(productDetail.getTitle()).contains("Ladies")){
                walmartProduct.setGender("Female");
            }
            else if(getValue(productDetail.getTitle()).contains("Men")){
                walmartProduct.setGender("Male");
            }
            else{
                walmartProduct.setGender("Unisex");
            }
//            设置图片信息
            walmartProduct.setAdditionalImageURL(getValue(productDetail.getImageURL1()));
            walmartProduct.setAdditionalImageURL1(getValue(productDetail.getImageURL3()));
            walmartProduct.setAdditionalImageURL2(getValue(productDetail.getImageURL4()));
            walmartProduct.setAdditionalImageURL3(getValue(productDetail.getImageURL5()));
            walmartProduct.setAdditionalImageURL4(getValue(productDetail.getImageURL6()));
            walmartProduct.setAdditionalImageURL5(getValue(productDetail.getImageURL7()));
            walmartProduct.setAdditionalImageURL6(getValue(productDetail.getImageURL8()));
            walmartProduct.setAdditionalImageURL7(getValue(productDetail.getImageURL9()));
            walmartProduct.setModelNumber(getValue(productDetail.getTitle()).substring(0,54)+" 2024");
//            设置价格
            walmartProduct.setMsrp(walmartSuggestedPrice);
//            设置货物尺码单位
            walmartProduct.setUnit("in");
//            设置季节编码
            walmartProduct.setSeasonCode("0- Basic");
//            设置商品颜色
            walmartProduct.setColorCategory("  ");
//            是否有认证证书
            walmartProduct.setGotsCertification("No");
//            设置年龄段
            walmartProduct.setAgeGroup("Adult");
            walmartProduct.setAgeGroup1("Child");
            walmartProduct.setAgeGroup2("Infant");
            walmartProduct.setAgeGroup3("Newborn");
            walmartProduct.setAgeGroup4("Preemie");
            walmartProduct.setAgeGroup5("Senior");
            walmartProduct.setAgeGroup6("Teen");
            walmartProduct.setAgeGroup7("Toddler");
            walmartProduct.setAgeGroup8("Tween");
//            加州政策
            walmartProduct.setCaliforniaProp65WarningText("None");
//            五点描述
            List<String> features = Arrays.asList(getValue(productDetail.getFivePointDescription()).split("\n"));
//            设置原始国
            walmartProduct.setCountryOfOriginTextiles("Imported");
            walmartProduct.setWarrantyURL("");
            walmartProduct.setSheerness("Opaque");
            walmartProduct.setLegOpeningCut("At Hip");
            walmartProduct.setMeasure("32");
            for (int i = 0; i < features.size(); i++) {
                String feature = features.get(i);
                if (!feature.trim().isEmpty()) {
                    if (i == 0) walmartProduct.setKeyFeatures(feature);
                    else if (i == 1) walmartProduct.setKeyFeatures1(feature);
                    else if (i == 2) walmartProduct.setKeyFeatures2(feature);
                    else if (i == 3) walmartProduct.setKeyFeatures3(feature);
                    else if (i == 4) walmartProduct.setKeyFeatures4(feature);
                    else if (i == 5) walmartProduct.setKeyFeatures5(feature);
                }
            }
            walmartProduct.setClothingSize(getValue(productDetail.getSize()));
//            保存
            walmartProductExcelList.add(walmartProduct);
        });
        return walmartProductExcelList;
    }
    @Override
    public List<InventoryUpdateData> getInventoryUpdateData() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<InventoryUpdateData> inventoryUpdateDataList = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            InventoryUpdateData data = new InventoryUpdateData();
//            设置sku
            if(SpringAiApplication.shopName!=null){
                data.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                data.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                data.setSku(getValue(productDetail.getSKU()));
            }
            data.setSku(getValue(productDetail.getSKU()));
            data.setNewQuantity(50);
            data.setFulfillmentCenterId("FC-10001593572");
            inventoryUpdateDataList.add(data);
        }
        return inventoryUpdateDataList;
    }
    @Override
    public List<PricePromoData> getPricePromoData() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<PricePromoData> pricePromoDataList = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            PricePromoData pricePromoData = new PricePromoData();
            //            设置sku
            if(SpringAiApplication.shopName!=null){
                pricePromoData.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                pricePromoData.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                pricePromoData.setSku(getValue(productDetail.getSKU()));
            }
//          productDetail.getWalmartSuggestedPriceUSD() 返回的是一个包含价格信息的字符串
            String walmartSuggestedPriceUSD = productDetail.getWalmartSuggestedPriceUSD();
//          更灵活的正则表达式，匹配所有数字和小数点组合，直到遇见 "USD"
            Pattern pattern = Pattern.compile("(\\d+\\.\\d+) USD");
            Matcher matcher = pattern.matcher(walmartSuggestedPriceUSD);
            Double walmartSuggestedPrice = 0.0;
            if (matcher.find()) {
                // 获取匹配到的数字
                walmartSuggestedPrice = Double.parseDouble(matcher.group(1));
            }
//          设置到 pricePromoData
            pricePromoData.setSellingPrice(((Double)(Math.round(walmartSuggestedPrice * 0.55 * 100) / 100.0)).toString());
            pricePromoDataList.add(pricePromoData);
        }
        return pricePromoDataList;
    }
    @Override
    public List<SkuLagTimeData> getSkuLagTimeData() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<SkuLagTimeData> skuLagTimeDataList = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            SkuLagTimeData skuLagTimeData = new SkuLagTimeData();
            //            设置sku
            if(SpringAiApplication.shopName!=null){
                skuLagTimeData.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                skuLagTimeData.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                skuLagTimeData.setSku(getValue(productDetail.getSKU()));
            }
            skuLagTimeData.setLagTime(1);
            skuLagTimeDataList.add(skuLagTimeData);
        }
        return skuLagTimeDataList;
    }

    @Override
    public List<SkuShippingTemplateData> getSkuShippingTemplateData() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<SkuShippingTemplateData> skuShippingTemplateDataList = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            SkuShippingTemplateData skuShippingTemplateData = new SkuShippingTemplateData();
            //            设置sku
            if(SpringAiApplication.shopName!=null){
                skuShippingTemplateData.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                skuShippingTemplateData.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                skuShippingTemplateData.setSku(getValue(productDetail.getSKU()));
            }
//          productDetail.getWalmartSuggestedPriceUSD() 返回的是一个包含价格信息的字符串
            String walmartSuggestedPriceUSD = productDetail.getWalmartSuggestedPriceUSD();
//          更灵活的正则表达式，匹配所有数字和小数点组合，直到遇见 "USD"
            Pattern pattern = Pattern.compile("(\\d+\\.\\d+) USD");
            Matcher matcher = pattern.matcher(walmartSuggestedPriceUSD);
            Double walmartSuggestedPrice = 0.0;
            if (matcher.find()) {
                // 获取匹配到的数字
                walmartSuggestedPrice = Double.parseDouble(matcher.group(1));
            }
//          设置到 pricePromoData
            Double v = (Math.round(walmartSuggestedPrice * 0.45 * 100) / 100.0);
            if(v==1.99){
                skuShippingTemplateData.setShippingTemplateId("202409999000431695");
            }
            if(v==2.99){
                skuShippingTemplateData.setShippingTemplateId("202404999000193588");
            }
            if(v==3.99){
                skuShippingTemplateData.setShippingTemplateId("202404999000195050");
            }
            if(v==4.99){
                skuShippingTemplateData.setShippingTemplateId("202404999000193534");
            }
            if(v==5.99){
                skuShippingTemplateData.setShippingTemplateId("202406999000290588");
            }
            if(v==6.99){
                skuShippingTemplateData.setShippingTemplateId("202404999000190334");
            }
            if(v==7.99){
                skuShippingTemplateData.setShippingTemplateId("202404999000194682");
            }
            if(v==8.99){
                skuShippingTemplateData.setShippingTemplateId("202405999000233253");
            }
            if(v==9.99){
                skuShippingTemplateData.setShippingTemplateId("202409999000431703");
            }
            skuShippingTemplateData.setActionType("Add");
            skuShippingTemplateData.setFulfillmentCenterId("10001593572");
            skuShippingTemplateDataList.add(skuShippingTemplateData);
        }
        return skuShippingTemplateDataList;
    }

    @Override
    public List<WalmartPromoData> getWalmartPromoData() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<WalmartPromoData> walmartPromoDataList = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            WalmartPromoData walmartPromoData = new WalmartPromoData();
            //            设置sku
            if(SpringAiApplication.shopName!=null){
                walmartPromoData.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                walmartPromoData.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                walmartPromoData.setSku(getValue(productDetail.getSKU()));
            }
            walmartPromoData.setPromoSettingAction("Create");
            walmartPromoData.setPromoType("Clearance");
//          productDetail.getWalmartSuggestedPriceUSD() 返回的是一个包含价格信息的字符串
            String walmartSuggestedPriceUSD = productDetail.getWalmartSuggestedPriceUSD();
//          更灵活的正则表达式，匹配所有数字和小数点组合，直到遇见 "USD"
            Pattern pattern = Pattern.compile("(\\d+\\.\\d+) USD");
            Matcher matcher = pattern.matcher(walmartSuggestedPriceUSD);
            Double walmartSuggestedPrice = 0.0;
            if (matcher.find()) {
                // 获取匹配到的数字
                walmartSuggestedPrice = Double.parseDouble(matcher.group(1));
            }
//          设置到 pricePromoData
            Double v = Math.round((walmartSuggestedPrice * 0.55) * 100) / 100.0;
            v = Math.round(v * 0.9 * 100) / 100.0;
            walmartPromoData.setPromoPrice(v.toString());
            walmartPromoData.setPromoPriceStartDate("2024/10/23 0:00:00");
            walmartPromoData.setPromoPriceEndDate("2024/12/31 0:00:00");
            walmartPromoDataList.add(walmartPromoData);
        }
        return walmartPromoDataList;
    }

    @Override
    public List<ProductReturnStatus> getProductReturnStatus() {
        List<ProductDetails> filteredProductDetails = SpringAiApplication.skuProductList.stream()
                .filter(productDetail -> !StringUtils.isEmpty(productDetail.getImageURL1()))
                .collect(Collectors.toList());
        List<ProductReturnStatus> productReturnStatuses = new ArrayList<>();
        for (ProductDetails productDetail : filteredProductDetails) {
            ProductReturnStatus productReturnStatus = new ProductReturnStatus();
            //            设置sku
            if(SpringAiApplication.shopName!=null){
                productReturnStatus.setSku(getValue(productDetail.getSKU())+"_"+SpringAiApplication.shopName);
            }
            else if(SpringAiApplication.shopId!=null){
                productReturnStatus.setSku(productDetail.getSKU()+"_"+SpringAiApplication.shopId);
            }
            else{
                productReturnStatus.setSku(getValue(productDetail.getSKU()));
            }
            productReturnStatus.setKeepIt("Yes");
            productReturnStatus.setReturnsCenterAlias("Monroe Township");
            productReturnStatuses.add(productReturnStatus);
        }
        return productReturnStatuses;
    }

    private String getValue(String value){
        if(StringUtils.isEmpty(value)){
            return "";
        }
        else{
            return value;
        }
    }
}
